/**
 * Created by zzy on 2017/1/13.
 */
var $ = require('jquery');
require('layout/util');
var $alert = require('libs/alert');
var $form = require('js-path/formValide.js');
var $form2 = require('js-path/formValideNotSub.js');
var wms = false;
var wmsId = 4;
$(function(){
    pageInit();
});
function pageInit() {
    var id = $.getUrlParam('id');
    loadAndInit(id);
}
function loadAndInit(id) {
    $.ajax({
        url:'api/v1/p/mms/user/detail',
        type:'post',
        data:{
            id:id
        },
        dataType:"json",
        success:function (result) {
            if(result.code==200){
                var data = result.data;
                dataInit(data);
            }else{
                $alert(result.message);
            }
        }
    })
}
function dataInit(data) {
    $("#id").val(data.id);
    $("#username").val(data.username);
    $("#name").val(data.name);
    $.each($("#system_id input[type=radio]"),function (index,obj) {
        if($(this).val()==data.systemId){
            $("#system_id input[type=radio]").eq(index).attr("checked","checked");
            warehouseController($(this),data.warehouseId);
            return;
        }
    });
    $("#last_login").val(data.lastLogin);
    $("#deadline").val(data.deadline);
}
function warehouseController($this,warehouseId) {
    if($this.val()==wmsId){
        wms = true;
    }else{
        wms = false;
    }
    if(wms){
        warehouseInit(warehouseId);
        $("#select_warehouse").attr("name","warehouseId");
        $("#div_warehouse").show();
    }else{
        $("#select_warehouse").attr("name","");
        $("#div_warehouse").hide();
    }
}
function warehouseInit(warehouseId) {
    $.ajax({
        url:'api/v1/p/crm/warehouse/query',
        type:'post',
        data:{
            warehouseType:"DOME"
        },
        dataType:"json",
        success:function (result) {
            $("#select_warehouse").html("<option value=''>全部</option>");
            $.each(result.data,function (index,obj) {
                if(warehouseId==obj.warehouseId){
                    $("#select_warehouse").append("<option value='"+obj.warehouseId+"' selected>"+obj.warehouseName+"</option>");
                }else{
                    $("#select_warehouse").append("<option value='"+obj.warehouseId+"'>"+obj.warehouseName+"</option>");
                }
            })
        }
    })
}
